/*****************************************************************************
 * hal_stm32l1_time.c
 *
 * Copyright (C) 2019 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "kernel/plum_kernel_api.h"
#include "hal/source/hal_stm32l1/hal_stm32l1.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

struct stm32_tim {
    plum_u32          id;
    TIM_HandleTypeDef handle;
    plum_void (*irq_handle)(plum_void *arg);
};

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

PLUM_PRIVATE struct stm32_tim tims[] = {
#if defined(TIM0)
    {.id = PLUM_HAL_TIME_ID(0), .handle.Instance = TIM0},
#endif

#if defined(TIM1)
    {.id = PLUM_HAL_TIME_ID(1), .handle.Instance = TIM1},
#endif

#if defined(TIM2)
    {.id = PLUM_HAL_TIME_ID(2), .handle.Instance = TIM2},
#endif

#if defined(TIM3)
    {.id = PLUM_HAL_TIME_ID(3), .handle.Instance = TIM3},
#endif

#if defined(TIM4)
    {.id = PLUM_HAL_TIME_ID(4), .handle.Instance = TIM4},
#endif

#if defined(TIM5)
    {.id = PLUM_HAL_TIME_ID(5), .handle.Instance = TIM5},
#endif

#if defined(TIM6)
    {.id = PLUM_HAL_TIME_ID(6), .handle.Instance = TIM6},
#endif

#if defined(TIM7)
    {.id = PLUM_HAL_TIME_ID(7), .handle.Instance = TIM7},
#endif

#if defined(TIM8)
    {.id = PLUM_HAL_TIME_ID(8), .handle.Instance = TIM8},
#endif
};

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE struct stm32_tim *hal_stm32_tim_instance(plum_u32 id)
{
    struct stm32_tim *TIMx = NULL;

    plum_s8 loop;
    for (loop = 0; loop < PLUM_ARRAY_CNT(stm32_tim); loop++) {
        if (id = stm32_tim[loop].id) {
            TIMx = &stm32_tim[loop];
            break;
        }
    }

    return (TIMx);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

plum_s32 plum_hal_time_init(plum_u32 id, plum_hal_time_t *config,
                            time_cb handle)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        struct stm32_tim *TIMx = hal_stm32_tim_instance(id);
        if (!TIMx) {
            LOG_E("The time id is error");
            rc = PLUM_ECODE_EIO;
            break;
        }

        if (!config) {
            LOG_E("The time config is NULL");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        if (!handle) {
            LOG_E("The time callback is NULL");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        TIMx->irq_handle = handle;

    } while (0);

    return (rc);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
